<template>
	<view>
		<view class="">
			<img src="@/static/study.jpg" alt="" style="width: 100%;height: 100%;" />
		</view>


		<view class="stu-tip">
			请输入报名预留电话
		</view>
		<view class="stu-form">
			<u-form :model="form" ref="uForm">
				<u-form-item prop="username" label-width="200rpx" label="预留电话" :required="true">
					<u-input v-model="form.username" placeholder="请填写预留电话" />
				</u-form-item>
				<u-form-item prop="code" label-width="200rpx" label="验证码" :required="true">
					<!-- <u-field v-model="form.code" > -->
					<u-input v-model="form.code" placeholder="请填写验证码" />
					<u-button size="mini" slot="right" @click="getCode">{{codeText}}</u-button>
					<!-- </u-field> -->
					<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
				</u-form-item>
				<u-form-item label="身份" prop="standing" label-width="200rpx" :required="true">
					<u-radio-group v-model="form.standing" placement="row" @change="groupChange">
						<u-radio style="margin-right: 10px;" label="家长" name="0"> 家长</u-radio>
						<u-radio style="margin-right: 10px;" label="学员" name="1"> 学员</u-radio>
					</u-radio-group>



				</u-form-item>
			</u-form>
			<u-button @click="submit" type="primary">提交</u-button>
		</view>

	</view>
</template>

<script>
	import {
		getCodeRequest,
		studentBingding
	} from '@/utils/api.js'
	export default {
		data() {
			return {
				form: {
					username: '',
					code: '',
					standing: ''
				},
				codeText: '',
				rules: {
					username: [{
						required: true,
						message: '请填写正确手机号码',
						trigger: ['change', 'blur'],
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value) || this.$u.test.landline(value);
						},

					}],
					code: [{

						required: true,
						message: '请填写验证码',
						trigger: ['change', 'blur'],
					}],
					standing: [{
						required: true,
						message: '请选择身份',
						trigger: ['change', 'blur'],
					}],
				}
			}
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(async (valid) => {
					if (valid) {
						console.log('验证通过');
						const res = await studentBingding(this.form)
						if(res.code === 200){
							uni.reLaunch({
								url:'/pages/index/index'
							})
						}
						
					} else {
						console.log('验证失败');
					}
				});
			},
			groupChange(e) {
				// console.log(e);
			},
			codeChange(text) {
				this.codeText = text;
			},
			getCode() {
				if(!this.form.username){
					uni.showToast({
						title:'请输入电话',
						icon:'none'
					})
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(async () => {
						uni.hideLoading();
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						const data = {
							phones: this.form.username
						}
						const res = await getCodeRequest(data)
					}, 1000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},

		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>
<style>
	page {
		background-color: #ffffff;
	}
</style>
<style>
	.top-left {
		margin-top: 50rpx;
		margin-left: 20rpx;
	}

	.left-title {
		font-size: 34rpx;
		font-weight: bold;
	}

	.title-intro {
		font-size: 24rpx;
		margin-top: 10rpx;
	}

	.stu-tip {
		margin-top: 30rpx;
		margin-left: 20rpx;
		font-weight: bold;
	}

	.stu-form {
		width: 90%;
		margin: 0 auto;
	}
</style>